<template>
    <div class="body">
        <!-- {{$store.getters.checkList}} -->
        <!-- {{$store.getters.cartList}} -->
        <!-- {{$store.getters.wxUserInfo}} -->
        <div class="address-content">
            <router-link to="/addressList" class="add-address" v-if="$store.getters.wxUserInfo.company_address">
                <p class="address">
                    {{$store.getters.wxUserInfo.company_address.province}}{{$store.getters.wxUserInfo.company_address.city}}{{$store.getters.wxUserInfo.company_address.area}}{{$store.getters.wxUserInfo.company_address.address}}
                </p>
                <p class="name">
                    {{$store.getters.wxUserInfo.company_address.name}}
                     {{$store.getters.wxUserInfo.company_address.phone}}
                </p>
            </router-link>
            <router-link to="/addressList" v-else class="add-address">
                <p><i class="iconfont icon-shouqi"></i>选择地址</p>
            </router-link>
            <i class="iconfont icon-gengduo"></i>
        </div>
        <div class="content">
            <van-cell title="余额抵扣" :value="'可用￥'+$store.getters.wxUserInfo.integral">
                <template #right-icon>
                    <van-switch v-model="isYue" active-color="#3DCEB9" size="24" :disabled='$store.getters.wxUserInfo.integral < allPrice/1000'/>
                </template>
            </van-cell>
        </div>
        <div class="content">
            <van-card
            v-for="(item,index) of goodsList"
            :key="index"
            :num="item.num"
            :price="getPrice(item)"
            :desc="item.goods_info"
            :title="item.goods_name"
            :thumb="item.image"
            />
        </div>
        <div class="content">
            <van-field
                v-model="remark"
                rows="2"
                autosize
                label="留言"
                type="textarea"
                maxlength="50"
                placeholder="请输入留言"
                show-word-limit
            />
        </div>
        <van-submit-bar :price="allPrice" button-text="提交订单" @submit="onSubmit" text-align="left"/>
    </div>
</template>
<script>
import {newOrder,pay,UserInfo} from '../ajax'
import { Toast } from 'vant';
import {wxPay} from '../wxPay'
export default {
    data() {
        return {
            isYue:false,
            goodsList:[],
            allPrice:0,
            remark:''
        }
    },
    created(){
        // console.log(this.$store.getters.cartList);
        console.log(this.$store.getters.wxUserInfo);
        this.getGoodsList();

    },
    methods:{
        getPrice(item){
            // console.log(this.$store.getters.wxUserInfo.is_believe && item.is_activity);
            return this.$store.getters.wxUserInfo.is_believe && item.is_activity?item.act_price:this.$store.getters.wxUserInfo.is_believe?item.price:item.ot_price
        },
        getGoodsList(){
            let arr = [];
            let price = 0;
            for(let i of this.$store.getters.cartList){
                for(let a of this.$store.getters.checkList){
                    if(i.cart_id == a){
                        arr.push(i)
                        price+=parseFloat(this.getPrice(i))*i.num*100
                    }
                }
            }
            console.log(arr);
            this.allPrice = price;
            this.goodsList = arr;
        },
        onSubmit(){
            if(!this.$store.getters.wxUserInfo.company_address){
                Toast('请选择收货地址');
                return;
            }
            if(this.$store.getters.wxUserInfo.integral < this.allPrice.toFixed(2)/100 && this.isYue){
                Toast('余额不足,请选择其他支付方式');
                return;
            }
            let goodsData = [];
            for(let i of this.goodsList){
                goodsData.push({
                    goods_id:i.goods_id,
                    center_id:i.center_id,
                    center_name:i.center_name,
                    num:i.num,
                    price:this.getPrice(i),
                    is_activity:i.is_activity,
                    total:(this.getPrice(i)*parseInt(i.num)).toFixed(2)
                })
            }
            const data = {
                cart_id:this.$store.getters.checkList.toString(),
                goodsData:JSON.stringify(goodsData),
                uid:this.$store.getters.wxUserInfo.id,
                addressID:this.$store.getters.wxUserInfo.company_address.id,
                total:this.allPrice.toFixed(2)/100,
                admin_id:this.$store.getters.admin_id,
                remark:this.remark,
                role_type:this.$store.getters.wxUserInfo.role_type,
                group_id:this.$store.getters.wxUserInfo.group_id,
                is_believe:this.$store.getters.wxUserInfo.is_believe,
                pay_type:this.isYue?1:this.$store.getters.wxUserInfo.is_believe?2:0//1.余额支付，2:授信支付 ，0:微信支付
            }
            console.log(data);
            newOrder(data).then(res=>{
                if(!data.is_believe){
                    if(data.pay_type){
                        UserInfo({openid:this.$store.getters.wxUserInfo.openid}).then(res=>{
                            console.log(res);
                            // console.log(this.$store.getters.wxUserInfo);
                            this.$store.commit('updateYue',res.integral);
                            this.$router.push({path:'/orderStatus',query:{state:1}});
                            return;
                        })
                    }else{
                        const data = {
                            mastr_order_no:res.order_no,
                            openid:this.$store.getters.wxUserInfo.openid,
                            type: 1 //1.微信支付 2.余额支付
                        }
                        pay(data).then(res=>{
                            wxPay(res).then(res=>{
                                window.location.href = process.env.NODE_ENV=="development"?`http://${window.location.host}/#/orderStatus?state=1`:`http://${window.location.host}/sxplat/#/orderStatus?state=1`
                            }).catch(err=>{
                                window.location.href = process.env.NODE_ENV=="development"?`http://${window.location.host}/#/orderStatus?state=0`:`http://${window.location.host}/sxplat/#/orderStatus?state=0`
                            })
                        })
                    }
                }else{
                    if(data.pay_type){
                        UserInfo({openid:this.$store.getters.wxUserInfo.openid}).then(res=>{
                            console.log(res);
                            // console.log(this.$store.getters.wxUserInfo);
                            this.$store.commit('updateYue',res.integral);
                            this.$router.push({path:'/orderStatus',query:{state:1}});
                            return;
                    })}else{
                        this.$router.push({path:'/orderStatus',query:{state:1}})
                    }
                }
            })
        }
    }
}
</script>
<style lang="scss" scoped>
    $green:#3DCEB9;
    $black:#333;
    .body{
        background: #eee;
        height: calc(100vh - 6rem);
        overflow: auto;
        .address-content{
            background: #fff;
            width: 94%;
            padding: 3%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            .address{
                padding:0.4rem 0;
                font-size: 1.4rem;
                font-weight: 800;
                line-height: 1.4rem;
            }
            .name{
                line-height: 2rem;
            }
        }
        .content{
            width: 100%;
            margin-top: 0.4rem;
            .van-cell__title{
                font-size: 1rem;
                font-weight: 500;
            }
            .van-cell,.van-cell__value{
                color: #999;
                margin-right: 1rem;
            }
            .van-card:not(:first-child) {
                margin-top: 1px;
            }
        }
        .van-submit-bar__bar{
            position: relative;
            .van-button{
                border-radius: 0;
                height: 100%;
                position: absolute;
                right: 0;
                bottom: 0;
                background: $green;
            }
        }
        .add-address{
            color: $black;
            .iconfont{
                margin-right: 0.4rem;
            }
        }
    }

</style>